<?php
class PrintersController extends AppController {

	var $name = 'Printers';

	function index() {
		$this->Printer->recursive = 0;
		$this->set('printers', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid printer', true));
			$this->redirect($this->referer());
		}
		$this->set('printer', $this->Printer->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Printer->create();
			if ($this->Printer->save($this->data)) {
				$this->Session->setFlash(__('The printer has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('The printer could not be saved. Please, try again.', true));
			}
		}
		$networks = $this->Printer->Network->find('list');
		$this->set(compact('networks'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid printer', true));
			$this->redirect($this->referer());
		}
		if (!empty($this->data)) {
			if ($this->Printer->save($this->data)) {
				$this->Session->setFlash(__('The printer has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('The printer could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Printer->read(null, $id);
		}
		$networks = $this->Printer->Network->find('list');
		$this->set(compact('networks'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for printer', true));
			$this->redirect($this->referer());
		}
		if ($this->Printer->delete($id)) {
			$this->Session->setFlash(__('Printer deleted', true));
			$this->redirect($this->referer());
		}
		$this->Session->setFlash(__('Printer was not deleted', true));
		$this->redirect($this->referer());
	}
}
?>